//购物车中的数据
<template>
  <div id="shoppingOne">
    <el-row
      v-for="(one, index) in shopData"
      :key="index"
      class="row"
      @click.native="checkItem(one.gId)"
    >
      <el-col :span="2">
        <div class="grid-content">
          <input type="checkbox" value="one.SCRId" v-model="one.checked" @click.stop/>
        </div>
      </el-col>

      <el-col :span="9">
        <div class="grid-content">
          <img :src="one.gPic" alt="" class="gPic" />
          <div class="gName">
            <span>{{ one.gName }}</span>
          </div>
        </div>
      </el-col>
      <el-col :span="2"
        ><div class="grid-content">￥{{ one.gPrice }}</div></el-col
      >

      <el-col :span="5">
        <el-input-number
          v-model="one.buyNum"
          :min="1"
          :max="one.gNum"
          class="numm"
        ></el-input-number>
        <!-- <div class="grid-content">{{one.SCRGoodsNum}}</div> -->
      </el-col>
      <el-col :span="2"
        ><div class="grid-content">
          ￥{{ one.gPrice * one.SCRGoodsNum }}
        </div></el-col
      >
      <el-col :span="4">
        <div class="grid-content">
          <el-button
            type="warning"
            icon="el-icon-star-off"
            circle
            @click.stop="delCollect(index, one.gId)"
            v-if="one.collected"
          ></el-button>
          <el-button
            type="success"
            icon="el-icon-star-off"
            circle
            @click.stop="addCollect(index, one.gId)"
            v-else
          ></el-button>

          <el-button
            type="danger"
            icon="el-icon-delete"
            circle
            @click.stop="deleteThis(one.SCRId)"
          ></el-button>
        </div>
      </el-col>
    </el-row>
  </div>
</template>
<script>
export default {
  inject: ["reload"],
  data() {
    return {};
  },
  methods: {
    collectedcheck(i, gid) {
      this.$axios
        .post("/api/collection/check", {
          gId: gid,
          uName: this.$store.state.user.userName,
        })
        .then((res) => {
          if (res.data.data[0].num == 0) {
            this.shopData[i].collected = false;
          } else {
            this.shopData[i].collected = true;
          }
        });
    },
    addCollect(index, gId) {
      this.$axios
        .post("/api/collection/addMes", {
          gId: gId,
          uName: this.$store.state.user.userName,
        })
        .then((res) => {
          if (res.data.code == "200") {
            this.$message({
              message: "收藏成功！",
              type: "success",
            });
            this.collectedcheck(index, gId);
          } else {
            this.$message.error("收藏失败!");
          }
        })
        .catch((err) => {
          console.log("err:", err);
        });
    },
    delCollect(index, gId) {
      this.$axios
        .post("/api/collection/delChoice", {
          gId: gId,
          uName: this.$store.state.user.userName,
        })
        .then((res) => {
          if (res.data.code == "200") {
            this.$message({
              message: "取消收藏成功",
              type: "success",
            });
            this.collectedcheck(index, gId);
          } else {
            this.$message.error("取消收藏失败!");
          }
        })
        .catch((err) => {
          console.log("err", err);
        });
    },
    checkItem(id) {
      let gId = id;
      this.$axios
        .get("/api/goods/gHitAdd", {
          params: {
            gId: gId,
          },
        })
        .then((res) => {
          this.$router.push({
            path: `/detail?gId=${gId}`,
          });
        })
        .catch((err) => {
          console.log(err);
        });
    },
    deleteThis(SCRId) {
      this.$axios
        .post("/api/shoppingCart/delMes", { SCRId: SCRId })
        .then((res) => {
          if (res.data.code == "200") {
            this.$message({
              message: "删除成功！",
              type: "success",
            });
            this.reload();
          } else {
            this.$message.error("删除失败!");
          }
        })
        .catch((err) => {
          console.log("err:", err);
        });
    },
  },
  watch: {
    shopData: {
      handler: function (newVal, oldVal) {
      },
      deep: true,
    },
  },
  mounted() {
    // console.log('子组件获取：',JSON.parse(JSON.stringify(this.shopData)));
  },
  props: ["shopData"],
};
</script>
<style scoped>
.row:hover {
  cursor: pointer;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}
.gPic {
  float: left;
  width: 125px;
  height: 125px;
  margin: 10px 20px;
}
.gName {
  float: right;
  width: 200px;
  margin: 60px 20px;
  line-height: 25px;
}
.grid-content {
  padding: 10px 0;
  line-height: 145px;
  text-align: center;
}
.numm {
  margin-top: 63px;
}
</style>